/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.bradipao.deschartsdemo.spinner.util;

import ohos.agp.utils.Color;
import ohos.agp.window.service.DisplayAttributes;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 其他util工具类
 *
 * @since 2021-08-02
 */
public class Utils {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "Utils");
    private static final int SYSTEM_FACTOR = 255;
    private static final int RADIX_HEX = 16;
    private static final int BIT_HEX = 0xFF;
    private static final String COVER_BIT = "0";
    private static DisplayAttributes mMetrics;
    /**
     * Darkens a color by a given factor.
     *
     * @param color the color to darken
     * @param factor The factor to darken the color.
     * @return darker version of specified color.
     */
    public static int darker(int color, float factor) {
        return Color.argb(Color.alpha(color),
                Math.max((int) (Integer.parseInt(red(color)) * factor), 0),
                Math.max((int) (Integer.parseInt(green(color)) * factor), 0),
                Math.max((int) (Integer.parseInt(blue(color)) * factor), 0));
    }

    /**
     * Lightens a color by a given factor.
     *
     * @param color The color to lighten
     * @param factor The factor to lighten the color. 0 will make the color unchanged. 1 will make the
     * color white.
     *
     * @return lighter version of the specified color.
     */
    public static int lighter(int color, float factor) {
        int red = (int) ((Integer.parseInt(red(color), RADIX_HEX) * (1 - factor) / SYSTEM_FACTOR + factor) * SYSTEM_FACTOR);
        int green = (int) ((Integer.parseInt(green(color), RADIX_HEX) / (double)SYSTEM_FACTOR + factor) * SYSTEM_FACTOR);
        int blue = (int) ((Integer.parseInt(blue(color), RADIX_HEX) * (1 - factor) / SYSTEM_FACTOR + factor) * SYSTEM_FACTOR);
        return Color.argb(Color.alpha(color), red, green, blue);
    }

    /**
     * 获取颜色值的red值
     *
     * @param color 颜色
     * @return red值
     */
    public static String red(int color) {
        String toHexString = Integer.toHexString((color >> RADIX_HEX) & BIT_HEX);
        if (toHexString.length() == 1) {
            toHexString = COVER_BIT + toHexString;
        }
        return toHexString;
    }

    /**
     * 获取颜色值的green值
     *
     * @param color 颜色
     * @return green值
     */
    public static String green(int color) {
        String toHexString = Integer.toHexString((color >> RADIX_HEX) & BIT_HEX);
        if (toHexString.length() == 1) {
            toHexString = COVER_BIT + toHexString;
        }
        return toHexString;
    }

    /**
     * 获取颜色值的blue值
     *
     * @param color 颜色
     * @return blue值
     */
    public static String blue(int color) {
        String toHexString = Integer.toHexString(color & BIT_HEX);
        if (toHexString.length() == 1) {
            toHexString = COVER_BIT + toHexString;
        }
        return toHexString;
    }

    /**
     * 获取颜色值的int类型red值
     *
     * @param color 颜色
     * @return int类型red值
     */
    public static int redInt(int color) {
        String colorStr = red(color);
        return string2Int(colorStr);
    }

    /**
     * 获取颜色值的int类型green值
     *
     * @param color 颜色
     * @return green值
     */
    public static int greenInt(int color) {
        String colorStr = green(color);
        return string2Int(colorStr);
    }

    /**
     * 获取颜色值的int类型blue值
     *
     * @param color 颜色
     * @return int类型blue值
     */
    public static int blueInt(int color) {
        String colorStr = blue(color);
        return string2Int(colorStr);
    }

    /**
     * 带有#的颜色转换为int类型颜色值
     *
     * @param colorStr 颜色
     * @return int类型颜色值
     */
    public static int string2Int(String colorStr) {
        return Integer.parseInt(colorStr, RADIX_HEX);
    }

    /**
     * Check if layout direction is RTL
     *
     * @param context the current context
     * @return {@code true} if the layout direction is right-to-left
     */
    public static boolean isRtl(Context context) {
        return context.getResourceManager().getConfiguration().isLayoutRTL;
    }

    /**
     * convertDpToPixel
     *
     * @param dp dp
     * @return float
     */
    public static float convertDpToPixel(float dp) {
        if (mMetrics == null) {
            return dp;
        }
        return dp * mMetrics.densityPixels;
    }
}
